<template>
    <div class="filter">
        <div class="filter-item">
            <label for="">所属单位：</label>
            <el-select
                v-model="searchForm.depId"
                class="m-2"
                style="width: 145px"
                :disabled="localTypeUnit != '1'"
            >
                <el-option
                    v-for="item in depIdList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
            </el-select>
        </div>
        <div class="filter-item">
            <label for="">商品名称：</label>
            <el-input
                v-model="searchForm.productName"
                placeholder="请输入"
                style="width: 145px;"
            />
        </div>
        <div class="filter-item">
            <label for="">商品类别：</label>
            <el-select
                v-model="searchForm.categoryName"
                class="m-2"
                style="width: 145px"
            >
                <el-option
                    v-for="item in categoryNameList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
            </el-select>
        </div>
        <div class="filter-item">
            <label for="">库存状态：</label>
            <el-select
                v-model="searchForm.status"
                class="m-2"
                style="width: 145px"
            >
                <el-option
                    v-for="item in statusList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
            </el-select>
        </div>
        <div class="filter-item">
            <el-button
                size="default"
                type="primary"
                @click="search"
            >查询</el-button>
            <el-button
                size="default"
                @click="reset"
            >重置</el-button>
        </div>
    </div>

    <div class="filter">
        <div class="filter-item">
            <el-button
                type="primary"
                @click="handleClick('add')"
            >新增</el-button>
        </div>
    </div>

    <el-table
        :data="tableData"
        style="width: 100%"
        id="inspectionTask"
    >
        <el-table-column
            prop="depId"
            label="所属单位"
        >
            <template #default="scope">
                {{ depNameTranslate(scope.row.depId) }}
            </template>
        </el-table-column>
        <!-- <el-table-column
            prop="depName"
            label="所属单位"
        /> -->
        <el-table-column
            prop="productCode"
            label="商品编号"
        />
        <el-table-column
            prop="productName"
            label="商品名称"
        />
        <el-table-column
            prop="categoryName"
            label="商品类别"
        />
        <el-table-column
            prop="specification"
            label="规格型号"
        />
        <el-table-column
            prop="unit"
            label="单位"
            width="90"
        />
        <el-table-column
            prop="currentStock"
            label="当前库存"
            width="90"
        />
        <el-table-column
            prop="status"
            label="库存状态"
            width="90"
        />
        <el-table-column
            fixed="right"
            label="操作"
            width="180"
        >
            <template #default="scope">

                <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="详情"
                    placement="top-start"
                >
                    <el-button
                        type="primary"
                        :icon="View"
                        size="small"
                        style="margin-bottom: 4px;"
                        @click="handleClick('detail', scope.row)"
                    >
                    </el-button>
                </el-tooltip>
                <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="编辑"
                    placement="top-start"
                >
                    <el-button
                        type="primary"
                        :icon="Edit"
                        size="small"
                        style="margin-bottom: 4px;margin-left: 4px;"
                        @click="handleClick('edit', scope.row)"
                    >
                    </el-button>
                </el-tooltip>
                <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="入库"
                    placement="top-start"
                >
                    <el-button
                        type="primary"
                        :icon="Sell"
                        size="small"
                        style="margin-bottom: 4px;margin-left: 4px;"
                        @click="handleClick('entry', scope.row)"
                    >
                    </el-button>
                </el-tooltip>
                <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="出库"
                    placement="top-start"
                >
                    <el-button
                        type="primary"
                        :icon="SoldOut"
                        size="small"
                        style="margin-bottom: 4px;margin-left: 4px;"
                        @click="handleClick('out', scope.row)"
                    >
                    </el-button>
                </el-tooltip>
                <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="盘点"
                    placement="top-start"
                >
                    <el-button
                        type="primary"
                        :icon="Search"
                        size="small"
                        style="margin-bottom: 4px;margin-left: 0px;"
                        @click="handleClick('review', scope.row)"
                    >
                    </el-button>
                </el-tooltip>
                <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="库存记录"
                    placement="top-start"
                >
                    <el-button
                        type="primary"
                        :icon="Finished"
                        size="small"
                        style="margin-bottom: 4px;margin-left: 4px;"
                        @click="handleClick('record', scope.row)"
                    >
                    </el-button>
                </el-tooltip>
                <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="删除"
                    placement="top-start"
                >
                    <el-button
                        type="danger"
                        :icon="Delete"
                        size="small"
                        style="margin-bottom: 4px;margin-left: 4px;"
                        @click="handleClick('delete', scope.row)"
                    >
                    </el-button>
                </el-tooltip>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="searchForm.pageNo"
        v-model:page-size="searchForm.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="paginationStyle"
    />

    <el-dialog
        v-model="addVisible"
        :title="currentType == 'add' ? '新增' : currentType == 'edit' ? '编辑' : currentType == 'detail' ? '详情' : ''"
        width="900"
    >
        <div
            class="dialogContent"
            style="padding-right: 24px;width: 840px"
        >
            <el-form
                ref="ruleFormRef"
                :model="ruleForm"
                label-width="120px"
                class="demo-ruleForm"
                :inline="true"
                :disabled="currentType == 'detail'"
            >
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="所属单位"
                            prop="depId"
                            style="width: 100%;"
                        >
                            <el-select
                                v-model="ruleForm.depId"
                                class="m-2"
                                :disabled="localTypeUnit != '1'"
                            >
                                <el-option
                                    v-for="item in depIdList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            label="商品编号"
                            prop="productCode"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="ruleForm.productCode"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            label="商品名称"
                            prop="productName"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="ruleForm.productName"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="商品类别"
                            prop="title"
                            style="width: 100%;"
                        >
                            <el-select
                                v-model="ruleForm.categoryName"
                                class="m-2"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in categoryNameList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            label="规格型号"
                            prop="specification"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="ruleForm.specification"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            label="单位"
                            prop="unit"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="ruleForm.unit"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="安全库存"
                            prop="safetyStock"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="ruleForm.safetyStock"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="addVisible = false">取消</el-button>
                <el-button
                    type="primary"
                    v-show="currentType != 'detail'"
                    @click="handleConfirm()"
                >
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <el-dialog
        v-model="entryVisible"
        title="入库"
        width="900"
    >
        <div
            class="dialogContent"
            style="padding-right: 24px;width: 840px"
        >
            <el-form
                ref="configFormRef"
                :model="entryRuleForm"
                label-width="120px"
                class="demo-ruleForm"
                :inline="true"
                :disabled="currentType == 'detail' || !entryVisibleDisabled"
            >
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="商品名称："
                            prop="productName"
                            style="width: 100%;"
                        >
                            <span>{{entryRuleForm.productName}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="入库类型："
                            prop="subType"
                            style="width: 100%;"
                        >
                            <el-select
                                v-model="entryRuleForm.subType"
                                class="m-2"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in entryList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col
                        :span="8"
                        v-show="entryRuleForm.subType == '采购入库'"
                    >
                        <el-form-item
                            label="采购单号："
                            prop="orderNumber"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="entryRuleForm.orderNumber"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col
                        :span="8"
                        v-show="entryRuleForm.subType == '归还入库'"
                    >
                        <el-form-item
                            label="归还人："
                            prop="recipient"
                            style="width: 100%;"
                        >
                            <el-select
                                v-model="entryRuleForm.recipient"
                                class="m-2"
                                placeholder="请选择"
                                filterable
                            >
                                <el-option
                                    v-for="item in userList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            :label="entryRuleForm.subType == '采购入库' ? '采购日期：' : '归还日期：'"
                            prop="useDate"
                            style="width: 100%;"
                        >
                            <el-date-picker
                                v-model="entryRuleForm.useDate"
                                format="YYYY-MM-DD"
                                type="date"
                                :size="'default'"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            :label="entryRuleForm.subType == '采购入库' ? '采购数量：' : '归还数量：'"
                            prop="quantity"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="entryRuleForm.quantity"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item
                            :label="entryRuleForm.subType == '采购入库' ? '采购说明：' : '归还说明：'"
                            prop="purpose"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="entryRuleForm.purpose"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="entryVisible = false">取消</el-button>
                <el-button
                    type="primary"
                    v-show="currentType != 'detail' && entryVisibleDisabled"
                    @click="handleConfirm()"
                >
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <el-dialog
        v-model="outVisible"
        title="出库"
        width="900"
    >
        <div
            class="dialogContent"
            style="padding-right: 24px;width: 840px"
        >
            <el-form
                ref="configFormRef"
                :model="entryRuleForm"
                label-width="120px"
                class="demo-ruleForm"
                :inline="true"
                :disabled="currentType == 'detail' || !outVisibleDisabled"
            >
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="商品名称："
                            prop="productName"
                            style="width: 100%;"
                        >
                            <span>{{entryRuleForm.productName}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="出库类型："
                            prop="subType"
                            style="width: 100%;"
                        >
                            <el-select
                                v-model="entryRuleForm.subType"
                                class="m-2"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in outList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col
                        :span="8"
                        v-show="entryRuleForm.subType == '退货出库'"
                    >
                        <el-form-item
                            label="采购单号："
                            prop="orderNumber"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="entryRuleForm.orderNumber"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col
                        :span="8"
                        v-show="entryRuleForm.subType == '领用出库'"
                    >
                        <el-form-item
                            label="领用人："
                            prop="recipient"
                            style="width: 100%;"
                        >
                            <el-select
                                v-model="entryRuleForm.recipient"
                                class="m-2"
                                placeholder="请选择"
                                filterable
                            >
                                <el-option
                                    v-for="item in userList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            :label="entryRuleForm.subType == '领用出库' ? '领用日期：' : entryRuleForm.subType == '退货出库' ? '退货日期：' : '报废日期'"
                            prop="useDate"
                            style="width: 100%;"
                        >
                            <el-date-picker
                                v-model="entryRuleForm.useDate"
                                format="YYYY-MM-DD"
                                type="date"
                                :size="'default'"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            :label="entryRuleForm.subType == '领用出库' ? '领用数量：' : entryRuleForm.subType == '退货出库' ? '退货数量：' : '报废数量'"
                            prop="quantity"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="entryRuleForm.quantity"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item
                            :label="entryRuleForm.subType == '领用出库' ? '用途说明：' : entryRuleForm.subType == '退货出库' ? '退货说明：' : '报废说明'"
                            prop="purpose"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="entryRuleForm.purpose"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="outVisible = false">取消</el-button>
                <el-button
                    type="primary"
                    v-show="currentType != 'detail' && outVisibleDisabled"
                    @click="handleConfirm()"
                >
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <el-dialog
        v-model="reviewVisible"
        title="盘点"
        width="900"
    >
        <div
            class="dialogContent"
            style="padding-right: 24px;width: 840px"
        >
            <el-form
                ref="configFormRef"
                :model="entryRuleForm"
                label-width="120px"
                class="demo-ruleForm"
                :inline="true"
                :disabled="currentType == 'detail' || !reviewVisibleDisabled"
            >
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="商品名称："
                            prop="productName"
                            style="width: 100%;"
                        >
                            <span>{{entryRuleForm.productName}}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item
                            label="账面数量："
                            prop="productName"
                            style="width: 100%;"
                        >
                            <span>{{entryRuleForm.currentStock}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item
                            label="实际数量："
                            prop="purpose"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="entryRuleForm.actualQty"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="16">
                        <el-form-item
                            label="差异原因："
                            prop="purpose"
                            style="width: 100%;"
                        >
                            <el-input
                                v-model="entryRuleForm.reason"
                                placeholder="请输入"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="reviewVisible = false">取消</el-button>
                <el-button
                    type="primary"
                    v-show="currentType != 'detail' && reviewVisibleDisabled"
                    @click="handleConfirm()"
                >
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <el-dialog
        v-model="recordVisible"
        title="库存记录"
        width="900"
    >
        <div
            class="dialogContent"
            style="padding-right: 24px;width: 840px"
        >

            <el-tabs
                v-model="recordActive"
                class="demo-tabs"
            >
                <el-tab-pane
                    label="出库单"
                    name="出库"
                >
                    <Out
                        v-if="recordVisible"
                        @detail="handleView"
                        :productId="currentProductId"
                    ></Out>
                </el-tab-pane>
                <el-tab-pane
                    label="入库单"
                    name="入库"
                >
                    <Entry
                        v-if="recordVisible"
                        @detail="handleView"
                        :productId="currentProductId"
                    ></Entry>
                </el-tab-pane>
                <el-tab-pane
                    label="盘点单"
                    name="盘点"
                >
                    <Review
                        v-if="recordVisible"
                        @detail="handleView"
                        :productId="currentProductId"
                    ></Review>
                </el-tab-pane>
            </el-tabs>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="reviewVisible = false">取消</el-button>
                <el-button
                    type="primary"
                    v-show="currentType != 'detail'"
                    @click="handleConfirm()"
                >
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
// utils
import inventoryHttp from "@/https/inventory.js";
import Storage from "@/utils/storage.js";
import userHttp from "@/https/user.js";
import {
    Delete,
    Edit,
    View,
    Sell,
    Finished,
    Search,
    SoldOut
} from "@element-plus/icons-vue";
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import moment from 'moment';
import Out from './components/out.vue';
import Entry from './components/entry.vue';
import Review from './components/review.vue';

let searchForm = ref({
    depId: Storage.localGet("depId"),
    productName: "",
    categoryName: "",
    status: "",
    pageNo: 1,
    pageSize: 10,
});
const localTypeUnit = ref(Storage.localGet("depId"));
// dict
const depIdList = ref([
    { label: "公司", value: "1" },
    { label: "星沙厂", value: "2" },
    { label: "城西厂", value: "3" },
    { label: "城北厂", value: "4" },
    { label: "城南厂", value: "5" },
    { label: "城东厂", value: "6" },
]);
const categoryNameList = ref([
    { label: "化学试剂类", value: "化学试剂类" },
    { label: "样品耗材类", value: "样品耗材类" },
    { label: "仪器耗材类", value: "仪器耗材类" },
    { label: "玻璃器皿类", value: "玻璃器皿类" },
    { label: "防护与清洁类", value: "防护与清洁类" },
    { label: "辅助用品类", value: "辅助用品类" },
    { label: "其他", value: "其他" },
]);
const statusList = ref([
    { label: "正常", value: "正常" },
    { label: "低库存", value: "低库存" },
    { label: "缺货", value: "缺货" },
]);
const entryList = ref([
    { label: "采购入库", value: "采购入库" },
    { label: "归还入库", value: "归还入库" },
]);
const outList = ref([
    { label: "领用出库", value: "领用出库" },
    { label: "退货出库", value: "退货出库" },
    { label: "报废出库", value: "报废出库" },
]);
const depNameTranslate = (depId) => {
    let depName = "";
    depIdList.value.forEach((item) => {
        if (item.value == depId) {
            depName = item.label;
        }
    });
    return depName;
};
let tableData = ref([]);
let total = ref(0);
const handleSizeChange = (val) => {
    searchForm.pageSize = val;
    search();
};
const handleCurrentChange = (val) => {
    searchForm.pageNo = val;
    search();
};
const userList = ref([]);
const userQueryUserList = async (row) => {
    let params = {
        depId: searchForm.value.depId,
        pageNo: 1,
        pageSize: 10000,
    };
    const res = await userHttp.userQueryUserList(params);
    if (res.data.code == 200) {
        res.data.data.records.forEach((item) => {
            item.label = item.realName;
            item.value = item.id + "";
        });
        userList.value = res.data.data.records;
    }
};
// 重置
const reset = async () => {
    searchForm.value.depId = Storage.localGet("depId")
    searchForm.value.productName = ""
    searchForm.value.categoryName = ""
    searchForm.value.status = ""
    searchForm.value.pageNo = 1
    searchForm.value.pageSize = 10
    search()
}
// 查询
const search = async () => {
    const res = await inventoryHttp.queryInventoryList(searchForm.value)
    console.log(res.data.data, 'res')
    tableData.value = res.data.data.records
    total.value = res.data.data.total
}
// 入库/出库/盘点
const reviewVisible = ref(false)
const reviewVisibleDisabled = ref(true)
const entryVisible = ref(false)
const entryVisibleDisabled = ref(true)
const outVisible = ref(false)
const outVisibleDisabled = ref(true)
const entryRuleForm = ref({
    depId: Storage.localGet("depId"),
    productCode: '',
    productName: '',
    categoryName: '',
    specification: '',
    currentStock: '',
    productId: '',
    unit: '',
    safetyStock: '',
    orderNumber: '',
    subType: '',
    useDate: moment().format('YYYY-MM-DD'),
    quantity: '',
    purpose: '',
    recipient: '',
    actualQty: '',
    theoreticalQty: '',
    reason: '',
})
const entryRuleFormDefault = ref({
    depId: Storage.localGet("depId"),
    productCode: '',
    productName: '',
    categoryName: '',
    specification: '',
    currentStock: '',
    productId: '',
    unit: '',
    safetyStock: '',
    orderNumber: '',
    subType: '',
    useDate: moment().format('YYYY-MM-DD'),
    quantity: '',
    purpose: '',
    recipient: '',
    actualQty: '',
    theoreticalQty: '',
    reason: '',
})
// 库存记录
const recordVisible = ref(false)
const recordActive = ref('出库')
// 新增
const addVisible = ref(false)
const ruleForm = ref({
    depId: Storage.localGet("depId"),
    productCode: '',
    productName: '',
    categoryName: '',
    specification: '',
    currentStock: '',
    productId: '',
    unit: '',
    safetyStock: '',
})
const ruleFormDefault = ref({
    depId: Storage.localGet("depId"),
    productCode: '',
    productName: '',
    categoryName: '',
    specification: '',
    currentStock: '',
    productId: '',
    unit: '',
    safetyStock: '',
})
const currentType = ref('')
const ruleFormRef = ref('')
const currentProductId = ref('')
const handleClick = (type, row) => {
    currentType.value = type
    console.log(currentProductId.value, 'currentProductId.value')
    if (type == 'add') {
        addVisible.value = true
        ruleForm.value = { ...ruleFormDefault.value }
    } else if (type == 'detail' || type == 'edit') {
        currentProductId.value = row.id
        addVisible.value = true
        ruleForm.value = { ...row }
        ruleForm.value.depId = row.depId + ''
    } else if (type == 'delete') {
        currentProductId.value = row.id
        ElMessageBox.confirm("确认删除该数据吗？", "删除", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
        })
            .then(async () => {
                const { data } = await inventoryHttp.deleteProduct(row)
                if (data.code == 200) {
                    ElMessage({
                        message: "删除成功",
                        type: "success",
                    });
                    search()
                }
            })
    } else if (type == 'entry') {
        currentProductId.value = row.id
        entryVisible.value = true
        entryVisibleDisabled.value = true
        entryRuleForm.value = { ...entryRuleFormDefault.value }
        entryRuleForm.value.subType = '采购入库'
        entryRuleFormDefault.value.subType = '采购入库'
        entryRuleForm.value.productId = row.id
        entryRuleForm.value.productName = row.productName
    } else if (type == 'out') {
        currentProductId.value = row.id
        outVisible.value = true
        outVisibleDisabled.value = true
        entryRuleForm.value = { ...entryRuleFormDefault.value }
        entryRuleForm.value.subType = '领用出库'
        entryRuleFormDefault.value.subType = '领用出库'
        entryRuleForm.value.productId = row.id
        entryRuleForm.value.productName = row.productName
    } else if (type == 'review') {
        currentProductId.value = row.id
        reviewVisible.value = true
        reviewVisibleDisabled.value = true
        entryRuleForm.value = { ...entryRuleFormDefault.value }
        entryRuleForm.value.productId = row.id
        entryRuleForm.value.productName = row.productName
        entryRuleForm.value.currentStock = row.currentStock
        entryRuleForm.value.theoreticalQty = row.currentStock
    } else if (type == 'record') {
        currentProductId.value = row.id
        recordVisible.value = true

    }
}
const handleConfirm = async () => {
    if (currentType.value == 'add') {
        const res = await inventoryHttp.addProduct(ruleForm.value)
        if (res.data.code == 200) {
            ElMessage({
                type: 'success',
                message: '新增成功'
            })
            search()
            addVisible.value = false
        } else {
            ElMessage({
                type: 'error',
                message: res.data.msg
            })
        }
    } else if (currentType.value == 'edit') {
        const res = await inventoryHttp.updateProduct(ruleForm.value)
        if (res.data.code == 200) {
            ElMessage({
                type: 'success',
                message: '修改成功'
            })
            search()
            addVisible.value = false
        } else {
            ElMessage({
                type: 'error',
                message: res.data.msg
            })
        }
    } else if (currentType.value == 'entry') {
        entryRuleForm.value.useDate = moment(entryRuleForm.value.useDate).format('YYYY-MM-DD')
        const res = await inventoryHttp.addInboundRecord(entryRuleForm.value)
        if (res.data.code == 200) {
            ElMessage({
                type: 'success',
                message: '入库成功'
            })
            search()
            entryVisible.value = false
        } else {
            ElMessage({
                type: 'error',
                message: res.data.msg
            })
        }
    } else if (currentType.value == 'out') {
        entryRuleForm.value.useDate = moment(entryRuleForm.value.useDate).format('YYYY-MM-DD')
        const res = await inventoryHttp.addOutboundRecord(entryRuleForm.value)
        if (res.data.code == 200) {
            ElMessage({
                type: 'success',
                message: '出库成功'
            })
            search()
            outVisible.value = false
        } else {
            ElMessage({
                type: 'error',
                message: res.data.msg
            })
        }
    } else if (currentType.value == 'review') {
        const res = await inventoryHttp.addStocktakeRecord(entryRuleForm.value)
        if (res.data.code == 200) {
            ElMessage({
                type: 'success',
                message: '盘点成功'
            })
            search()
            reviewVisible.value = false
        } else {
            ElMessage({
                type: 'error',
                message: res.data.msg
            })
        }
    }
}
const handleView = async (params) => {
    if (params.operationType == 'out') {
        const res = await inventoryHttp.getRecordsByProductId(params.row)
        console.log('out', params.row.productId, res.data.data)
        outVisible.value = true
        outVisibleDisabled.value = false
    } else if (params.operationType == 'entry') {
        const res = await inventoryHttp.getRecordsByProductId(params.row)
        console.log('out', params.row.productId, res.data.data)
        entryVisible.value = true
        entryVisibleDisabled.value = false
    } else if (params.operationType == 'review') {
        const res = await inventoryHttp.getRecordsByProductId(params.row)
        console.log('out', params.row.productId, res.data.data)
        reviewVisible.value = true
        reviewVisibleDisabled.value = false
    }
}
onMounted(() => {
    userQueryUserList()
    search()
})
</script>